import { rmSync } from "fs";
import { resolve } from "path";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import electron from "vite-plugin-electron/simple";
import eslintPlugin from "vite-plugin-eslint";
import pkg from "./package.json";

rmSync("dist", { recursive: true, force: true }); // v14.14.0

// https://vitejs.dev/config/
export default defineConfig(() => {
  return {
    base: "./",
    build: {
      assetsDir: "static",
      rollupOptions: {
        input: {
          index: resolve(__dirname, "index.html"),
        },
        output: {
          chunkFileNames: `[name]_${pkg.version}.js`,
          assetFileNames: `static/[ext]/[name]_${pkg.version}.[ext]`,
        },
      },
      assetsInlineLimit: 1000,

      minify: true,
    },
    resolve: {
      alias: {
        "~tables": resolve(__dirname, "src/tables"),
        "~types": resolve(__dirname, "src/types"),
        "~components": resolve(__dirname, "components"),
        "~pages": resolve(__dirname, "src/pages"),
        "~excel": resolve(__dirname, "src/excel"),
      },
    },
    plugins: [
      vue(),
      vueJsx(),
      eslintPlugin(),
      electron({
        main: {
          vite: {
            build: {
              lib: {
                entry: "electron/main.ts",
                formats: ["cjs"],
                fileName: () => "[name].js",
              },
            },
          },
        },
        ...(!process.env.VITEST
          ? {
              renderer: {
                resolve: {
                  "better-sqlite3": { type: "cjs" },
                  typeorm: { type: "cjs" },
                  unzipper: { type: "cjs" },
                },
              },
            }
          : {}),
      }),
    ],
  };
});
